﻿using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Collections.Generic;


namespace Play
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("\n-----------------------------\n  Game of Nim  \n-----------------------------\n\n" +
                  "Use:   <Play full/prune> <List of heaps sizes>\n" +
                 "Example:   Play full (1,3,5) \n" +
                 "--------------------------------------------------------------------------------\n\n"
                  );


            //cmd line args checks
            if (args.Count() < 2)
            {
                Console.WriteLine("\nInvalid Command line   -   there are less than 2 arguments.  \n Example:  Play full (1,3,5)\n\n");
                return;
            }

            if (args[0] != "full" && args[0] != "prune")
            {
                Console.WriteLine("Invalid Command line   -   Not valid play type.\n  Available play types:  full / prune.   \n Example:  Play full (1,3,5)\n\n");
                return;
            }


            //Translate user "heaps" input into INT[] heaps
            List<int> heaps = new List<int>();
            string heapsInputString = args[1];
            bool finish = false;

            Match match = Regex.Match(heapsInputString, "\\d+");
            if (match.Success)
            {
                heaps.Add(Convert.ToInt32(match.Value));
            }
            else //invalid input
            {
                Console.WriteLine("Invalid Command line   -   Can't give empty \"heaps\" list.\n  Example:  Play full (1,3,5)\n\n");
                return;
            }

            while( ! finish )
            {
                match = match.NextMatch();
                if (match.Success)
                {
                    heaps.Add(Convert.ToInt32(match.Value));
                }
                else finish = true;
            }
            var heapsArray = heaps.ToArray();
//////////////////////////////////////////////////////////////////
//                User Configuraion 
//////////////////////////////////////////////////////////////////
            var miniMaxType = Convert.ToString(args[0]);

            Console.WriteLine("Calculating...");
            if (miniMaxType == "full")
            {
                var nimGame = new NimGame(heapsArray);
                nimGame.CalcMiniMax(MinimaxType.Full);
                nimGame.PrintRecommended();
            }
            else if(miniMaxType == "prune")
            {
                var nimGame2 = new NimGame(heapsArray);
                nimGame2.CalcMiniMax(MinimaxType.Prune);
                nimGame2.PrintRecommended();
            }
        }
    }
}
